import 'package:flutter/material.dart';

import 'package:flutter_kit_dsp/flutter_kit_dsp.dart';
import '../../tool/assets.dart';
import 'slope_item.dart';

class TypeItem extends StatefulWidget {
  Passmodel model;
  String name;
  final int flex1;
  final int flex2;
  final void Function(bool) onClick;
  TypeItem(
      {super.key,
      required this.flex1,
      required this.flex2,
      required this.name,
      required this.model,
      required this.onClick});

  @override
  State<TypeItem> createState() => _TypeItemState();
}

class _TypeItemState extends State<TypeItem> {
  int _pressed = 0;

  Color _img_bg_color(bool isAdd) {
    if (widget.model.select) {
      return (_pressed == 1 && !isAdd || _pressed == 2 && isAdd) ? Color_select_btn.withAlpha(70) : Color_select_btn;
    } else {
      return Color_select_btn.withAlpha(70);
    }
  }

  Color _text_color() {
    return widget.model.select ? Colors.white : Colors.grey;
  }

  Widget _btnItem(bool isAdd) {
    return SizedBox(
      width: 20,
      height: 20,
      child: GestureDetector(
        onTap: widget.model.select ? () => widget.onClick(isAdd) : null,
        onTapDown: widget.model.select ? (_) => _pressed = isAdd ? 2 : 1 : null,
        onTapUp: widget.model.select ? (_) => _pressed = 0 : null,
        child: Image.asset(
          color: _img_bg_color(isAdd),
          isAdd ? assets.arrow_right : assets.arrow_left,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        Flexible(flex: widget.flex1, child: passType(widget.name)),
        const SizedBox(width: 5),
        Flexible(
          flex: widget.flex2,
          child: Container(
              decoration: BoxDecoration(color: Color_bg_btn, borderRadius: BorderRadius.circular(4.0)),
              child: Row(
                children: [
                  ///左边箭头按钮
                  _btnItem(false),

                  ///中间文字
                  Expanded(
                    flex: 10,
                    child: Text(
                      widget.name == 'TYPE' ? widget.model.type!.name : '${widget.model.freq}',
                      textAlign: TextAlign.center,
                      style: TextStyle(color: _text_color(), fontSize: 12),
                    ),
                  ),

                  ///右边箭头按钮
                  _btnItem(true),
                ],
              )),
        ),
      ],
    );
  }
}
